﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Baza.EF.BLL;
using Baza.EF.DAL;

namespace Pep_3014_3070.Anonymous
{
    public partial class NewOwner : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCountry();
            }
        }
        private void BindCountry()
        {
            List<Countries> temp = DARegistration.getAllCountres();
            Countries c = new Countries();
            c.Country_name = "Choose country";
            c.CountryID = 0;
            temp.Insert(0, c);

            CountryDropDownList.DataSource = temp;
            CountryDropDownList.DataTextField = "Country_name";
            CountryDropDownList.DataValueField = "CountryID";
            CountryDropDownList.DataBind();
        }

        protected void RegisterButton_Click(object sender, EventArgs e)
        {

            if (FirstNameInput.Text != "" && LastNameInput.Text != "" && EmailInput.Text != "" && PasswordInput.Text != "" && AboutMeInput.Text !=""
                && Address1Input.Text != "" && Address2Input.Text !="" && CityInput.Text != "" && StreetNumberInput.Text != "" )
            {

                if (DAOwners.checkEmail(EmailInput.Text.Trim()))
                {


                    Baza.EF.DAL.Owners o = new Baza.EF.DAL.Owners();

                    o.FirstName = FirstNameInput.Text;
                    o.LastName = LastNameInput.Text;
                    o.HomeNumber = HomeNumberInput.Text;
                    o.WebsiteURL = WebsiteURLInput.Text;
                    o.FaxNumber = FaxNumberInput.Text;
                    o.Email = EmailInput.Text;
                    o.CellNumber = CellNumberInput.Text;
                    o.BusinessNumber = BusinessNumberInput.Text;
                    o.AdditionalContactInfo = BusinessNumberInput.Text;
                    o.TypeID = 1;
                    Address ad = new Address();
                    ad.Address1 = Address1Input.Text;
                    ad.Address2 = Address2Input.Text;
                    ad.StreetNumber = Convert.ToInt32(StreetNumberInput.Text);

                    Cities cit = new Cities();
                    cit.Name = CityInput.Text;
                    cit.CountryID = Convert.ToInt32(CountryDropDownList.SelectedIndex);

                    o.PasswordSalt = UIHelper.GenerateSalt();
                    o.PasswordHash = UIHelper.GenerateHash(PasswordInput.Text, o.PasswordSalt);
                    
                    o.Photo = "/Content/img/Owner.jpg";
                    o.AboutMe = AboutMeInput.Text;
                    o.Photo = "/Content/img/Owner.jpg";
                    int id = DARegistration.RegisterOwners(o, ad, cit);

                    Clear();

                    string url = string.Format("/ProfileOwner/{0}", id);
                    if (url != "" || url != null)
                        Response.Redirect(url);

                }
                else
                    AlertMessage("Email adresa je zauzeta!");

            }
        }

        private void Clear()
        {
            FirstNameInput.Text = "";
            LastNameInput.Text = "";
            HomeNumberInput.Text = "";
            WebsiteURLInput.Text = "";
            FaxNumberInput.Text = "";
            EmailInput.Text = "";
            CellNumberInput.Text = "";
            BusinessNumberInput.Text = "";
            BusinessNumberInput.Text = "";
            CountryDropDownList.SelectedIndex = 0;
            Address1Input.Text = "";
            Address2Input.Text = "";
            CityInput.Text = "";
            StreetNumberInput.Text = "";
            AdditionalContactInfoInput.Text = "";

        }

        private void AlertMessage(string errorMsg)
        {

            string message = "alert('" + errorMsg + "')";
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "alert", message, true);


        }
    }
}